//
//  CMDBCommentManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/6.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBCommentManager: NSObject {
    
    static let getInstance: CMDBCommentManager = CMDBCommentManager()
    
    private override init() {
        super.init()
        
    }
}
extension CMDBCommentManager: CMDBCommentManagerDelegate {
    
    func comment(commentParam: CMDBCommentParam, handler: CMDBCommentHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) throws {
        
        guard !commentParam.content.isEmpty else {
            
            throw CMDBCommonExceptionType.ExceptionCode(code: CMDBCommonExceptionCodeType.CommentContentEmpty)
        }
        CMDBHttpManager.getInstance.request(commentParam.URLString(), param: commentParam.keyValues, success: { (sysTime, data, page) in
            
            handler(string: (data as? String)!)
            
            }, error: error, failure: failure)
    }
}
// 获取更多评论
extension CMDBCommentManager {
    
    func queryMoreComments(moreCommentParam: CMDBCommentListParam,tableView: UITableView, handler: CMDBCommentListHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(moreCommentParam.URLString(), param: moreCommentParam.keyValues, ownPage: 1, tableView: tableView, success: { (sysTime, data, page) in
            
            let arr = CMDBCommentBean.objectArrayWithKeyValuesArray(data as! NSArray) as? Array<CMDBCommentBean>
            
            handler(commentList: arr)
            
            }, error: error, failure: failure)
    }
}